﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using System.Net.Sockets;
using System.Text;

namespace FRS_LIB.Devices
{
    public class DeviceBase : Device
    {
        // A delegate type for alive state change.
        
        public DeviceBase(string address, string name) : base(address, name)
        {
        }

        protected virtual byte[] TcpSendreceive(Byte[] bytes, int port)
        {
            if (bytes == null) return new byte[0];

            // Create a TcpClient.
            // Note, for this client to work you need to have a TcpServer 
            // connected to the same address as specified by the server, port
            // combination.
            TcpClient client = new TcpClientWithTimeout(Address, port, 5000).Connect();

            // Get a client stream for reading and writing.
            NetworkStream stream = client.GetStream();

            //Thread.Sleep(50);
            // Send the message to the connected TcpServer. 
            stream.Write(bytes, 0, bytes.Length);

            // Receive the TcpServer.response.

            // Buffer to store the response bytes.
            Byte[] data = new Byte[10];

            // Read the first batch of the TcpServer response bytes.
            stream.Read(data, 0, data.Length);

            // Close everything.
            stream.Close();
            client.Close();
            return data;
        }

        protected virtual string TcpSendreceive(string command, int port)
        {
            if (command == null) return Encoding.UTF8.GetString(new byte[0]);

            // Translate the passed message into ASCII and store it as a Byte array.
            byte[] bytes = Encoding.UTF8.GetBytes(command);
            
            // Create a TcpClient.
            // Note, for this client to work you need to have a TcpServer 
            // connected to the same address as specified by the server, port
            // combination.
            TcpClient client = new TcpClientWithTimeout(Address, port, 2000).Connect();

            // Get a client stream for reading and writing.
            NetworkStream stream = client.GetStream();
            

            // Send the message to the connected TcpServer. 
            stream.Write(bytes, 0, bytes.Length);

            // Receive the TcpServer.response.

            // Buffer to store the response bytes.
            Byte[] data = new Byte[256];
            
            // Read the first batch of the TcpServer response bytes.
            stream.Read(data, 0, data.Length);

            // Close everything.
            stream.Close();
            client.Close();


            return Encoding.UTF8.GetString(data);
        }

        public virtual bool GetOutput(int outputNr)
        {
            return false;
        }

        public virtual void SetOutput(int outputNr, int value)
        {
        }
    }
}
